package com.cms.recursoshumanos.json.object;

import com.cms.recursoshumanos.cache.UserInfoCache;
import com.cms.recursoshumanos.constant.Constant;
import com.cms.recursoshumanos.json.me.JSONArray;
import com.cms.recursoshumanos.json.me.JSONException;
import com.cms.recursoshumanos.json.me.JSONObject;
import com.cms.recursoshumanos.object.Hospital;
import com.cms.recursoshumanos.object.list.ListaHospital;

public class JsonObjectGetHospital 
{

	public static JsonObjectGetHospital _instance = null;
	public static JsonObjectGetHospital getInstance() {
		if(_instance == null)
			_instance = new JsonObjectGetHospital();
		
		return _instance;
	}
	public String Result;
	private static final String
	
		GETHOSPITAL = "GETHOSPITAL",
		GETHOSPITAL_STATUS = "STATUS",
		GETHOSPITAL_DATA = "DATA",
		GETHOSPITAL_DATOS_HOSPITAL = "DATOS_HOSPITAL",
		
		GETHOSPITAL_HOSPITAL_ID = "hospital_id",
		GETHOSPITAL_HOSPITAL_NOMBRE= "nombre_hospital",
		GETHOSPITAL_HOSPITAL_RIF = "rif_hospital",
		GETHOSPITAL_HOSPITAL_NRO_TLF_1 = "nro_tlf_hospital_1",
		GETHOSPITAL_HOSPITAL_NRO_TLF_2 = "nro_tlf_hospital_2",
		GETHOSPITAL_HOSPITAL_PERSONA_CONTACTO = "nombre_persona_contacto_hospital",
		GETHOSPITAL_HOSPITAL_NOMBRE_DIRECCION = "nombre_direccion";

	public void fromJSON (String jsonString) 
	{
		if (jsonString != null)
		{
			if (!jsonString.equals(""))
			{
				try
				{
					JSONObject jsonObject = new JSONObject(jsonString);		
					JSONObject getHospital = jsonObject.getJSONArray(GETHOSPITAL).getJSONObject(0);
					String status = getHospital.getString(GETHOSPITAL_STATUS);
					
					Result = status;
					
					if (status.equals(Constant.CODE_OK))
					{
		
						JSONArray data = getHospital.getJSONArray(GETHOSPITAL_DATA);
						
						JSONObject objeto_data = data.getJSONObject(0);
						JSONArray datos_hospital = objeto_data.getJSONArray(GETHOSPITAL_DATOS_HOSPITAL);
		
						ListaHospital listado = new ListaHospital();
						
						for (int i = 0; i < datos_hospital.length(); i++ )
						{
							
								
								JSONObject json_hospital = datos_hospital.getJSONObject(i);
						
								listado.addHospital(new Hospital(Integer.parseInt(json_hospital.getString(GETHOSPITAL_HOSPITAL_ID)), 
																	json_hospital.getString(GETHOSPITAL_HOSPITAL_NOMBRE), 
																	json_hospital.getString(GETHOSPITAL_HOSPITAL_RIF), 
																	json_hospital.getString(GETHOSPITAL_HOSPITAL_NRO_TLF_1), 
																	(json_hospital.getString(GETHOSPITAL_HOSPITAL_NRO_TLF_2).equals("null") ?
																			null :
																			json_hospital.getString(GETHOSPITAL_HOSPITAL_NRO_TLF_2)
																			), 
																	json_hospital.getString(GETHOSPITAL_HOSPITAL_PERSONA_CONTACTO), 
																	json_hospital.getString(GETHOSPITAL_HOSPITAL_NOMBRE_DIRECCION)));

						}
						UserInfoCache.getInstance().setFechaCache(7);
						UserInfoCache.getInstance().getPoliza().getSeguro().setListaHospital(listado);
						
					}
						
		
				} 
				catch (JSONException ex) 
				{
					ex.printStackTrace();
					Result = Constant.CODE_EMPTY;
				}
				catch (Exception ex) 
				{
					ex.printStackTrace();
					Result = Constant.CODE_EMPTY;
				}
			}
			else
			{
				Result = Constant.CODE_NOT_SERVER_RESPONSE;
			}
		}
		else
		{
			Result = Constant.CODE_NOT_SERVER_RESPONSE;
		}
	
		
	}

	public String getResult()
	{
		return Result;
	}
	
}



